#!/usr/bin/perl
use CGI;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
use Digest::MD5 qw(md5 md5_hex md5_base64);
my $readin;
read(STDIN, $readin, $ENV{'CONTENT_LENGTH'});
my $inputstring = $readin;
my $schar = substr($inputstring, 1, 1);






my %hash = ();


if($ENV{'CONTENT_LENGTH'}.length() < 1){
#####If nothing is typed right now.#####
	print "Content-type: text/html\n\n";
	
	print $inputstring; 
	open (FILE,"register.html");
	while (<FILE>) {
		print $_;
	}
	close(FILE);
print <<END_OF_PRINTING; 
END_OF_PRINTING
}
#register username/name/pass
elsif ($schar ne 's'){
	print "Content-type: text/html\n\n";
	my ($pairs,$key,$value);
	my @pairsarr = split(/&/, $inputstring);
	foreach my $pairs (@pairsarr){
		my @keyvalue = split(/=/, $pairs);
		$key = $keyvalue[0];
		$value = $keyvalue[1];
		$hash{ $key } = $value;
		open (FILE2, ">>url_sub.txt");
		print FILE2 $value;
		print FILE2 "\n";
		close (FILE2); 	
	}

	
	my $username = $hash{"LoginForm%5BEmail%5D"};
	$username =~ s/%40/at/g;
	my $password = $hash{"LoginForm%5BPassword%5D"};
	my $name = $hash{"LoginForm%5BName%5D"};
	my $command = "cat users/template > users/$username";
	system($command);
	open (MYFILE, ">>password.txt");
	print "\n\n";
	my $enter = "$username".",".md5_hex($password).",".$name;
	print MYFILE $enter;
	print MYFILE "\n";
	close (MYFILE);
			open (FILE,"login.html");
	while (<FILE>) {
		print $_;
	}
	close(FILE);
}
else{
}
